package com.sdkx.app.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.model.WayPoint;
import com.sdkx.app.util.ToastUtil;

import java.util.List;

public class CurAirportListAdapter  extends ArrayAdapter{
    private final String TAG = CurAirportListAdapter.class.getSimpleName();
    private List<WayPoint> datasource;
    private int resourceId;
    private int selectPosition=-1;

    public CurAirportListAdapter(@NonNull Context context, int resource, @NonNull List<WayPoint> objects) {
        super(context, resource,  objects);
        resourceId = resource;
        datasource = objects;
    }

    public void setSelectPosition(int position){
        this.selectPosition = position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        WayPoint wayPoint = datasource.get(position);
        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, viewGroup, false);
            holder = new ViewHolder(view);
            holder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onButtonClick(v.getId(),position);
                }
            });
            view.setTag(holder);
        } else {
            view = convertView;
            holder =(ViewHolder) view.getTag();
        }
        holder.tvWpname.setText(wayPoint.getName());
        if(selectPosition == position){
            if (holder.submenu.getVisibility() == View.VISIBLE)
                holder.submenu.setVisibility(View.GONE);
            else
                holder.submenu.setVisibility(View.VISIBLE);
        }else {
            holder.submenu.setVisibility(View.GONE);
        }
        return view;
    }
    private void onButtonClick(int buttonId,int position){
        switch (buttonId) {
            case R.id.button:
                Log.d(TAG,"click button,position: "+position);
                WayPoint point = datasource.get(position);
                SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getContext().getApplicationContext()).edit();
                editor.putInt("benJiJiChang",point.getId());
                editor.apply();
                ToastUtil.show(getContext(),"设置成功");
                break;
            default:
                break;
        }
    }

    protected class ViewHolder {
        private TextView tvWpname;
        private ImageView ivThreeMenuIcon;
        private Button button;
        private LinearLayout submenu;

        ViewHolder(View view) {
            tvWpname = (TextView) view.findViewById(R.id.tv_wpname);
            ivThreeMenuIcon = (ImageView) view.findViewById(R.id.iv_three_menu_icon);
            submenu = (LinearLayout) view.findViewById(R.id.submenu);
            button = (Button) view.findViewById(R.id.button);
        }
    }
}
